package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.system.service.IBkAIService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AI文章助手Controller
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/ai")
public class BkAIController extends BaseController
{
    @Autowired
    private IBkAIService bkAIService;

    /**
     * 生成文章摘要
     */
    @PostMapping("/generate-excerpt")
    public AjaxResult generateExcerpt(@RequestBody Map<String, String> params)
    {
        try {
            String content = params.get("content");
            String title = params.get("title");

            String excerpt = bkAIService.generateArticleExcerpt(content, title);

            Map<String, Object> result = new HashMap<>();
            result.put("excerpt", excerpt);

            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("生成AI摘要失败", e);
            return AjaxResult.error("生成摘要失败: " + e.getMessage());
        }
    }

    /**
     * 获取文章优化建议
     */
    @Log(title = "获取AI优化建议", businessType = BusinessType.OTHER)
    @PostMapping("/optimize-article")
    public AjaxResult getOptimizationSuggestions(@RequestBody Map<String, String> params)
    {
        try {
            String content = params.get("content");
            String title = params.get("title");
            String excerpt = params.get("excerpt");

            List<String> suggestions = bkAIService.getArticleOptimizationSuggestions(content, title, excerpt);

            Map<String, Object> result = new HashMap<>();
            result.put("suggestions", suggestions);

            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("获取AI优化建议失败", e);
            return AjaxResult.error("获取优化建议失败: " + e.getMessage());
        }
    }

    /**
     * 更新文章的AI摘要
     */
    @Log(title = "更新AI摘要", businessType = BusinessType.UPDATE)
    @PostMapping("/update-ai-excerpt")
    public AjaxResult updateAIExcerpt(@RequestBody Map<String, Object> params)
    {
        try {
            Long articleId = Long.parseLong(params.get("articleId").toString());
            String aiExcerpt = (String) params.get("aiExcerpt");

            boolean result = bkAIService.updateArticleAIExcerpt(articleId, aiExcerpt);

            if (result) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("更新AI摘要失败");
            }
        } catch (Exception e) {
            logger.error("更新AI摘要失败", e);
            return AjaxResult.error("更新AI摘要失败: " + e.getMessage());
        }
    }
}